using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using Xeml.Manager;
using Xeml.Document.Contracts;
using Xeml.Document;

namespace Xid.Controls
{
    public partial class AddOntologyWizard : ComponentFactory.Krypton.Toolkit.KryptonForm
    {

        private OntologyHandlerResource _handlerResource;
        private IDocument _xemlDoc;
        public OntologyHandlerResource AddedHandlerResource { get; private set; }
        public AddOntologyWizard(IDocument xemlDoc)
        {
            _xemlDoc = xemlDoc;
            InitializeComponent();
            kryptonNavigator1.SelectedIndex = 0;
            _handlerResource = new OntologyHandlerResource("temp");
            textBoxAlias.DataBindings.Add("Text", _handlerResource, "NameSpaceAlias");
            textBoxOntology.DataBindings.Add("Text", _handlerResource, "Location");

            kryptonNavigator1.SelectedPageChanged += new EventHandler(kryptonNavigator1_SelectedPageChanged);
            handlerCollectionBindingSource.CurrentChanged += new EventHandler(handlerCollectionBindingSource_CurrentChanged);
            comboBoxOntologyType.SelectedIndexChanged += new EventHandler(comboBoxOntologyType_SelectedIndexChanged);
            comboBoxOntologyType.SelectedIndex = 0;

        }

        void handlerCollectionBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            IOntologyHandler io = handlerCollectionBindingSource.Current as IOntologyHandler;
            textBoxHandlerUri.DataBindings.Clear();
            textBoxPublisher.DataBindings.Clear();
            textBoxVersion.DataBindings.Clear();
            textBoxDescription.DataBindings.Clear();
            textBoxAuthor.DataBindings.Clear();

            textBoxHandlerUri.Text = "-";
            textBoxPublisher.Text = "-";
            textBoxVersion.Text = "-";
            textBoxDescription.Text = "-";
            textBoxAuthor.Text = "-";

            if (io != null)
            {
                textBoxHandlerUri.DataBindings.Add("Text", io, "Uri");
                textBoxPublisher.DataBindings.Add("Text", io, "Publisher");
                textBoxVersion.DataBindings.Add("Text", io, "Version");
                textBoxDescription.DataBindings.Add("Text", io, "Description");
                textBoxAuthor.DataBindings.Add("Text", io, "Author");
            }
        }

        void comboBoxOntologyType_SelectedIndexChanged(object sender, EventArgs e)
        {


            HandlerCollection filterdHandler = new HandlerCollection();
            foreach (IOntologyHandler io in OntologyManager.Instance.OntologyHandler.Values)
            {
                if ((string)comboBoxOntologyType.SelectedItem == "Environment ontology")
                {
                    if (io.OntologyType == OntologyType.Environment)
                        filterdHandler.Add(io);
                }
                if ((string)comboBoxOntologyType.SelectedItem == "Plant structure ontology")
                {
                    if (io.OntologyType == OntologyType.OrganismStructure)
                        filterdHandler.Add(io);
                }
                if ((string)comboBoxOntologyType.SelectedItem == "Developmental ontology")
                {
                    if (io.OntologyType == OntologyType.Developmental)
                        filterdHandler.Add(io);
                }
                if ((string)comboBoxOntologyType.SelectedItem == "Positioning ontology")
                {
                    if (io.OntologyType == OntologyType.Positioning)
                        filterdHandler.Add(io);
                }
                if ((string)comboBoxOntologyType.SelectedItem == "Germplasm ontology")
                {
                    comboBoxOntologyHandler.Text = "Not supported yet.";
                }
            }

            handlerCollectionBindingSource.DataSource = filterdHandler;

        }

        void kryptonNavigator1_SelectedPageChanged(object sender, EventArgs e)
        {
            kryptonButtonBackToHandlerPage.Enabled = kryptonNavigator1.SelectedIndex != 0;
            if (kryptonNavigator1.SelectedIndex == 2)
            {
                kryptonButtonNextToFinish.Text = "Finish";
            }
            else
            {
                kryptonButtonNextToFinish.Text = "Next";
            }
        }

        private void kryptonButtonNextToFinish_Click(object sender, EventArgs e)
        {
            if (kryptonNavigator1.SelectedIndex < 2)
            {
                kryptonNavigator1.PerformNextAction();
            }
            else
            {
                this.ValidateChildren();
                bool valid = true;
                kryptonLabelError.Visible = false;
                errorProvider1.SetError(textBoxAlias, "");
                errorProvider1.SetError(textBoxOntology, "");

                IOntologyHandler io = handlerCollectionBindingSource.Current as IOntologyHandler;
                if (io != null)
                {

                    if (_xemlDoc.DocumentResources.Contains(_handlerResource.NameSpaceAlias, io.OntologyType))
                    {
                        errorProvider1.SetError(textBoxAlias, "The alias already exist.");
                        valid = false;
                    }
                    else
                    {
                        OntologyHandlerResource ohr = _xemlDoc.DocumentResources.Add(io.Uri, _handlerResource.NameSpaceAlias, _handlerResource.Location, true);
                        //_handlerResource.LoadComponent(io.Uri);
                        if (ohr.ComponentState != ComponentLoadState.Loaded)
                        {
                            valid = false;
                            kryptonLabelError.Text = "Unable to load handler. Component state is: " + ohr.ComponentState.ToString();
                            kryptonLabelError.Visible = true;
                        }
                        this.Enabled = false;
                        Cursor.Current = Cursors.WaitCursor;
                        ohr.LoadOntology();
                        this.Enabled = true;
                        Cursor.Current = Cursors.Default;

                        if (ohr.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                        {
                            errorProvider1.SetError(textBoxOntology, ohr.Handler.OntologyLoadMessage);
                            valid = false;
                        }
                        else
                            AddedHandlerResource = ohr;


                    }
                    if (valid)
                    {

                        DialogResult = DialogResult.OK;
                        //  _xemlDoc.DocumentResources.Remove(_handlerResource);
                        this.Close();
                    }




                    //if (io.OntologyType == OntologyType.Environment)
                    //{
                    //    if (_xemlDoc.DocumentResources.EnvironmentOntologyHandler.Contains(_handlerResource.NameSpaceAlias))
                    //    {
                    //        errorProvider1.SetError(textBoxAlias, "The alias already exist.");
                    //        valid = false;
                    //    }
                    //    else
                    //    {
                    //        _handlerResource.LoadComponent(io.Uri);
                    //        if (_handlerResource.ComponentState != ComponentLoadState.Loaded)
                    //        {
                    //            valid = false;
                    //            kryptonLabelError.Text = "Unable to load handler. Component state is: " + _handlerResource.ComponentState.ToString();
                    //            kryptonLabelError.Visible = true;
                    //        }
                    //        this.Enabled = false;
                    //        Cursor.Current = Cursors.WaitCursor;
                    //        _handlerResource.LoadOntology();
                    //        this.Enabled = true;
                    //        Cursor.Current = Cursors.Default;

                    //        if (_handlerResource.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                    //        {
                    //            errorProvider1.SetError(textBoxOntology, _handlerResource.Handler.OntologyLoadMessage);
                    //            valid = false;
                    //        }

                    //    }
                    //    if (valid)
                    //    {
                    //        _xemlDoc.DocumentResources.EnvironmentOntologyHandler.Add(_handlerResource);
                    //        this.Close();
                    //    }
                    //}

                    //if (io.OntologyType == OntologyType.OrganismStructure)
                    //{
                    //    if (_xemlDoc.DocumentResources.OrganismStructureOntologyHandler.Contains(_handlerResource.NameSpaceAlias))
                    //    {
                    //        errorProvider1.SetError(textBoxAlias, "The alias already exist.");
                    //        valid = false;
                    //    }
                    //    else
                    //    {
                    //        _handlerResource.LoadComponent(io.Uri);
                    //        if (_handlerResource.ComponentState != ComponentLoadState.Loaded)
                    //        {
                    //            valid = false;
                    //            kryptonLabelError.Text = "Unable to load handler. Component state is: " + _handlerResource.ComponentState.ToString();
                    //            kryptonLabelError.Visible = true;
                    //        }
                    //        this.Enabled = false;
                    //        Cursor.Current = Cursors.WaitCursor;
                    //        _handlerResource.LoadOntology();
                    //        this.Enabled = true;
                    //        Cursor.Current = Cursors.Default;

                    //        if (valid && _handlerResource.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                    //        {
                    //            errorProvider1.SetError(textBoxOntology, "Unable to load ontology, load message is:" + _handlerResource.Handler.OntologyLoadMessage);
                    //            valid = false;
                    //        }

                    //    }
                    //    if (valid)
                    //    {
                    //        _xemlDoc.DocumentResources.OrganismStructureOntologyHandler.Add(_handlerResource);
                    //        this.Close();
                    //    }
                    //}

                    //if (io.OntologyType == OntologyType.Developmental)
                    //{
                    //    if (_xemlDoc.DocumentResources.DevelopmentalOntologyHandler.Contains(_handlerResource.NameSpaceAlias))
                    //    {
                    //        errorProvider1.SetError(textBoxAlias, "The alias already exist.");
                    //        valid = false;
                    //    }
                    //    else
                    //    {
                    //        _handlerResource.LoadComponent(io.Uri);
                    //        if (_handlerResource.ComponentState != ComponentLoadState.Loaded)
                    //        {
                    //            valid = false;
                    //            kryptonLabelError.Text = "Unable to load handler. Component state is: " + _handlerResource.ComponentState.ToString();
                    //            kryptonLabelError.Visible = true;
                    //        }
                    //        this.Enabled = false;
                    //        Cursor.Current = Cursors.WaitCursor;
                    //        _handlerResource.LoadOntology();
                    //        this.Enabled = true;
                    //        Cursor.Current = Cursors.Default;

                    //        if (valid && _handlerResource.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                    //        {
                    //            errorProvider1.SetError(textBoxOntology, "Unable to load ontology, load message is:" + _handlerResource.Handler.OntologyLoadMessage);
                    //            valid = false;
                    //        }

                    //    }
                    //    if (valid)
                    //    {
                    //        _xemlDoc.DocumentResources.OrganismStructureOntologyHandler.Add(_handlerResource);
                    //        this.Close();
                    //    }

                    //}

                    //if (io.OntologyType == OntologyType.Positioning)
                    //{
                    //    if (_xemlDoc.DocumentResources.PositioningOntologyHandler.Contains(_handlerResource.NameSpaceAlias))
                    //    {
                    //        errorProvider1.SetError(textBoxAlias, "The alias already exist.");
                    //        valid = false;
                    //    }
                    //    else
                    //    {
                    //        _handlerResource.LoadComponent(io.Uri);
                    //        if (_handlerResource.ComponentState != ComponentLoadState.Loaded)
                    //        {
                    //            valid = false;
                    //            kryptonLabelError.Text = "Unable to load handler. Component state is: " + _handlerResource.ComponentState.ToString();
                    //            kryptonLabelError.Visible = true;
                    //        }
                    //        this.Enabled = false;
                    //        Cursor.Current = Cursors.WaitCursor;
                    //        _handlerResource.LoadOntology();
                    //        this.Enabled = true;
                    //        Cursor.Current = Cursors.Default;

                    //        if (valid && _handlerResource.Handler.OntologyLoadState != OntologyLoadState.Loaded)
                    //        {
                    //            errorProvider1.SetError(textBoxOntology, "Unable to load ontology, load message is:" + _handlerResource.Handler.OntologyLoadMessage);
                    //            valid = false;
                    //        }

                    //    }
                    //    if (valid)
                    //    {
                    //        _xemlDoc.DocumentResources.PositioningOntologyHandler.Add(_handlerResource);
                    //        this.Close();
                    //    }

                    //}
                }
                else
                {
                    kryptonLabelError.Text = "No handler selected";
                    kryptonLabelError.Visible = true;
                    valid = false;
                }
            }
        }

        private void kryptonButtonBackToHandlerPage_Click(object sender, EventArgs e)
        {
            kryptonNavigator1.PerformPreviousAction();
        }

        private void kryptonButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();

        }


    }

    internal class HandlerCollection : List<IXemlComponent>
    {
    }
}